﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Data;
using LiJu.Db;
using System.IO;
using System.Threading;

namespace Liju.Common
{
    /// <summary>
    /// 统一实例对象
    /// </summary>
    public static class UnifiedObj
    {

        /// <summary>
        /// 设备启用状态 0位移编码器 1电批 2IO控制器 true有 false无
        /// </summary>
        public static bool[] DeviceEnable = { false, false, true};

        /// <summary>
        /// 设备连接状态 0位移编码器 1电批 2IO控制器 -1异常 0未连接 1已连接
        /// </summary>
        public static int[] DeviceConState = { 0, 0, 0 };

        /// <summary>
        /// 编码器COM
        /// </summary>
        static string PositionPortName;

        /// <summary>
        /// 编码器坐标 XYZR 0123 编码器号 XYRZ 1234
        /// </summary>
        public static int[] arrayPosition = { 0, 0, 0, 0 };

        /// <summary>
        /// IO控制器 16个IN值
        /// </summary>
        public static bool[] arrInValue = { false, false, false, false, false, false, false, false, false, false, 
            false, false, false, false, false, false};

        /// <summary>
        /// 当前三色灯状态 -1初始化 1亮 0灭  
        /// </summary>
        public static int[] LedState = { -1, -1, -1, -1 };

        /// <summary>
        /// 当前套筒灯状态 -2初始化 -1未亮 0无料红 1有料绿
        /// </summary>
        public static int[] TaoTongLEDState = { -2, -2, -2, -2 };

        /// <summary>
        /// 当前套筒状态 -1初始化 1松开 0夹紧  
        /// </summary>
        public static int[] TaoTongIsOpen = { -1, -1, -1, -1 };
        
        /// <summary>
        /// 套筒推杆推出缩回运行时间ms
        /// </summary>
        public static int TuiGanRunTime = 100;

        /// <summary>
        /// 当前急停状态 -1初始化 1急停 0非急停 
        /// </summary>
        public static int nIsStop = -1;

        /// <summary>
        /// 电批是否允许作业 
        /// </summary>
        public static bool DianPiCanWork = false;

        /// <summary>
        /// 位移编码器COM口
        /// </summary>
        static SerialPort SerialPortWeiYi;// = new SerialPort();
                
        /// <summary>
        /// 位移编码器控制
        /// </summary>
        static ModbusRTU modbusRTU = new ModbusRTU();

        /// <summary>
        /// IO控制器
        /// </summary>
       static ModbusIO modbusIO ;

        public static int DeviceInit()
        {
            //设备通讯设置 
            DataTable dtComSet = Bll.GetComPortList();
            if (dtComSet.Rows.Count > 0)
            {
                if (DeviceEnable[0])//是否有位移编码器
                {
                    if (DeviceConState[0] < 1)
                    {
                        PositionPortName = dtComSet.Rows[0]["PortName"].ToString().Trim();
                        PositionPortInit();
                    }
                }

                if (DeviceEnable[1])//是否有电批 不能重复引用 在页面实现
                {
                    ////电批IP 端口
                    //LiJu.Common.Batch.ipaddress = dtComSet.Rows[1]["PortName"].ToString().Trim();
                    //LiJu.Common.Batch.ipport = Convert.ToInt32(dtComSet.Rows[1]["BaudRate"]);
                    ////连接 开始握手并订阅结果
                    //LiJu.Common.Batch.TCPConnection(null, null);
                    ////禁止使能
                    //LiJu.Common.Batch.DisEnable();
                }

                if (DeviceEnable[2])//是否有IO控制器
                {
                    modbusIO = new ModbusIO(dtComSet.Rows[2]["PortName"].ToString().Trim(), Convert.ToInt32(dtComSet.Rows[2]["BaudRate"]));
                    if (modbusIO.IOConState)
                    {
                        DeviceConState[2] = 1;
                    }
                    else
                    {
                        DeviceConState[2] = 0;
                    }
                }
                string strFileName  = System.IO.Directory.GetCurrentDirectory() + "\\Log\\Use\\";                
                if (!System.IO.Directory.Exists(strFileName))
                {
                    System.IO.Directory.CreateDirectory(strFileName);
                }
                strFileName = System.IO.Directory.GetCurrentDirectory() + "\\Log\\MES\\";
                if (!System.IO.Directory.Exists(strFileName))
                {
                    System.IO.Directory.CreateDirectory(strFileName);
                }

                return 1;
            }
            return -1;
        }

        /// <summary>
        /// 检查连接状态 未连则连接
        /// </summary>
        private static void CheckConnect()
        {

        }

        /// <summary>
        /// 位移编码器COM初始化
        /// </summary>
        public static void PositionPortInit()
        {
            try
            {
                SerialPortWeiYi = new SerialPort();
                //spWeiYi = new SerialPort(strPortName, 57600, Parity.None, 8, StopBits.One);
                SerialPortWeiYi.PortName = PositionPortName;
                SerialPortWeiYi.BaudRate = 115200;
                SerialPortWeiYi.Parity = Parity.None;
                SerialPortWeiYi.DataBits = 8;
                SerialPortWeiYi.StopBits = StopBits.One;
                SerialPortWeiYi.ReadTimeout = 1000; // 读取超时时间
                SerialPortWeiYi.WriteTimeout = 1000; // 写入超时时间
                SerialPortWeiYi.Open();
                if (SerialPortWeiYi.IsOpen)
                {
                    DeviceConState[0] = 1;
                    ModbusRTU.serialPort = SerialPortWeiYi;
                }
            }
            catch (Exception)
            {
                DeviceConState[0] = -1;//异常 写日志
            }
        }

        /// <summary>
        /// 获取编码器坐标
        /// </summary>
        /// <returns></returns>
        public static int[] GetPosition()
        {
            //是否启用位移编码器 已连接
            if (DeviceEnable[0] )
            {
                if (DeviceConState[0] != 1)
                {
                    CheckConnect();
                }
                if (DeviceConState[0] != 1)
                {
                    return arrayPosition;
                }

                //读取第一个编码器 X
                byte[] response1 = UnifiedObj.modbusRTU.ReadHoldingRegisters(1, 0, 1);
                //获取编码器值
                arrayPosition[0] = UnifiedObj.modbusRTU.ParseModbusFrame(BitConverter.ToString(response1));
                //arrayPosition[0] = UnifiedObj.modbusRTU.GetFrameNum(1, 0, 1);

                //读取第二个编码器 Y
                byte[] response2 = UnifiedObj.modbusRTU.ReadHoldingRegisters(2, 0, 1);
                //获取编码器值
                arrayPosition[1] = UnifiedObj.modbusRTU.ParseModbusFrame(BitConverter.ToString(response2));
                //arrayPosition[1] = UnifiedObj.modbusRTU.GetFrameNum(2, 0, 1);

                //读取第四个编码器（多圈）Z  
                byte[] response4 = UnifiedObj.modbusRTU.ReadHoldingRegisters(4, 0, 2);
                //获取编码器值
                arrayPosition[2] = UnifiedObj.modbusRTU.ParseModbusFrames(BitConverter.ToString(response4));

                //读取第三个编码器 水平垂直旋转 R
                byte[] response3 = UnifiedObj.modbusRTU.ReadHoldingRegisters(3, 0, 1);
                //获取编码器值
                arrayPosition[3] = UnifiedObj.modbusRTU.ParseModbusFrame(BitConverter.ToString(response3));
                //arrayPosition[3] = UnifiedObj.modbusRTU.GetFrameNum(3, 0, 1);

                ////编码器读值异常 重新连接
                //if (arrayPosition[0] < 1 || arrayPosition[1] < 1 || arrayPosition[2] < 1 || arrayPosition[3] < 1)
                //{
                //    ModbusRTU.serialPort.DiscardInBuffer();
                //    ModbusRTU.serialPort.Close();
                //    PositionPortInit();
                //    if (DeviceConState[0] == 1)
                //    {
                //        GetPosition();
                //    }
                //}

            }
            return arrayPosition;
        }

        #region 套筒操作 读取状态

        /// <summary>
        /// 套筒到位信号 套筒序号1-4 0有料 1无料
        /// </summary>
        /// <param name="TaoTongNo"></param>
        /// <returns></returns>
        public static bool GetTaoTongState(int TaoTongNo)
        {
            return !arrInValue[TaoTongNo - 1 ];//套筒序号1-4 对应0-3 值取反 0有料 1无料
        }

        /// <summary>
        /// 套筒红灯 isOpen true亮 false灭 套筒序号1-4
        /// </summary>
        /// <param name="TaoTongNo"></param>
        /// <returns></returns>
        public static bool SetTaoTongRed(bool isOpen, int TaoTongNo)
        {
            if (isOpen)
            {
                if (TaoTongLEDState[TaoTongNo - 1] != 0)
                {
                    if (TaoTongLEDState[TaoTongNo - 1] == 1 || TaoTongLEDState[TaoTongNo - 1] == -2)//绿灯亮 要先灭
                    {
                        SetTaoTongGreen(false, TaoTongNo);
                    }
                    TaoTongLEDState[TaoTongNo - 1] = 0;
                    return modbusIO.SetOutOpen(7 + TaoTongNo);//8-11
                }
            }
            else
            {
                if (TaoTongLEDState[TaoTongNo - 1] != -1)
                {
                    TaoTongLEDState[TaoTongNo - 1] = -1;
                    return modbusIO.SetOutClose(7 + TaoTongNo);//8-11
                }
            }
            return true;
        }

        /// <summary>
        /// 套筒绿灯 isOpen true亮 false灭 套筒序号1-4
        /// </summary>
        /// <param name="TaoTongNo"></param>
        /// <returns></returns>
        public static bool SetTaoTongGreen(bool isOpen, int TaoTongNo)
        {
            if (isOpen)
            {
                if (TaoTongLEDState[TaoTongNo - 1] != 1)
                {
                    if (TaoTongLEDState[TaoTongNo - 1] == 0|| TaoTongLEDState[TaoTongNo - 1] == -2)//红灯亮 要先灭
                    {
                        SetTaoTongRed(false, TaoTongNo);
                    }
                    TaoTongLEDState[TaoTongNo - 1] = 1;
                    return modbusIO.SetOutOpen(3 + TaoTongNo);//4-7
                }
            }
            else
            {
                if (TaoTongLEDState[TaoTongNo - 1] != -1)
                {
                    TaoTongLEDState[TaoTongNo - 1] = -1;
                    return modbusIO.SetOutClose(3 + TaoTongNo);//4-7
                }
            }
            return true;
        }

        /// <summary>
        /// 套筒松开/夹住 isOpen true松开 false夹住 套筒序号1-4
        /// </summary>
        /// <param name="TaoTongNo"></param>
        /// <returns></returns>
        public static bool SetTaoTongOpen(bool isOpen, int TaoTongNo)
        {
            if (isOpen)//松开 推杆缩回
            {
                if (TaoTongIsOpen[TaoTongNo - 1] != 1)//套筒未松开 才操作推杆
                {
                    TaoTongIsOpen[TaoTongNo - 1] = 1;
                    switch (TaoTongNo)
                    {
                        case 4:
                            modbusIO.SetOutOpen(22);
                            Thread.Sleep(TuiGanRunTime);
                            return modbusIO.SetOutClose(22);
                        case 3:
                            modbusIO.SetOutOpen(20);
                            Thread.Sleep(TuiGanRunTime);
                            return modbusIO.SetOutClose(20);
                        case 2:
                            modbusIO.SetOutOpen(14);
                            Thread.Sleep(TuiGanRunTime);
                            return modbusIO.SetOutClose(14);
                        case 1:
                            modbusIO.SetOutOpen(12);
                            Thread.Sleep(TuiGanRunTime);
                            return modbusIO.SetOutClose(12);
                    }
                }                
            }
            else//夹住 推杆推出
            {
                if (TaoTongIsOpen[TaoTongNo - 1] != 0)//套筒未夹住 才操作推杆
                {
                    TaoTongIsOpen[TaoTongNo - 1] = 0;
                    switch (TaoTongNo)
                    {
                        case 4:
                            modbusIO.SetOutOpen(23);
                            Thread.Sleep(TuiGanRunTime);
                            return modbusIO.SetOutClose(23);
                        case 3:
                            modbusIO.SetOutOpen(21);
                            Thread.Sleep(TuiGanRunTime);
                            return modbusIO.SetOutClose(21);
                        case 2:
                            modbusIO.SetOutOpen(15);
                            Thread.Sleep(TuiGanRunTime);
                            return modbusIO.SetOutClose(15);
                        case 1:
                            modbusIO.SetOutOpen(13);
                            Thread.Sleep(TuiGanRunTime);
                            return modbusIO.SetOutClose(13);
                    }
                }
            }
            return false;
        }

        #endregion

        #region 螺丝机 操作 读取状态

        /// <summary>
        /// 螺丝机发送送钉信号 螺丝机1-2 true 送钉 false 重置信号
        /// </summary>
        /// <returns></returns>
        public static bool SetLuoSiJiSongDing(bool isOpen, int LuoSiJiNo)
        {
            if (isOpen)
            {
                if (LuoSiJiNo == 1)
                {
                    return modbusIO.SetOutOpen(16);
                }
                else
                {
                    return modbusIO.SetOutOpen(17);
                }
            }
            else
            {
                if (LuoSiJiNo == 1)
                {
                    return modbusIO.SetOutClose(16);
                }
                else
                {
                    return modbusIO.SetOutClose(17);
                }
            }
        }

        /// <summary>
        /// 送钉成功信号 螺丝机1-2 实际无
        /// </summary>
        /// <param name="LuoSiJiNo"></param>
        /// <returns></returns>
        public static bool GetSongDingState(int LuoSiJiNo)
        {
            //if (LuoSiJiNo == 1)
            //{
            //    return arrInValue[4];//螺丝机1
            //}
            //else
            //{
            //    return arrInValue[6];//螺丝机2
            //}
            return true;
        }

        /// <summary>
        /// 螺丝机是否报警 螺丝机1-2 
        /// </summary>
        /// <param name="LuoSiJiNo"></param>
        /// <returns></returns>
        public static bool GetLuoSiJiWaring( int LuoSiJiNo)
        {            
            if (LuoSiJiNo == 1)
            {
                return arrInValue[5];//螺丝机1
            }
            else
            {
                return arrInValue[7];//螺丝机2
            }
        }

        /// <summary>
        /// 螺丝机 缺料满料状态 螺丝机1/2  0正常 1缺料 2满料
        /// </summary>
        /// <param name="LuoSiJiNo"></param>
        /// <returns></returns>
        public static int GetLuoSiJiState(int LuoSiJiNo)
        {
            if (LuoSiJiNo == 1)//螺丝机1
            {
                if (arrInValue[16])//缺料
                {
                    return 1;
                }
                else if (arrInValue[18])//满料
                {
                    return 2;
                }                
                return 0;
            }
            else
            {
                if (arrInValue[17])//缺料
                {
                    return 1;
                }
                else if (arrInValue[19])//满料
                {
                    return 2;
                }
                return 0;
            }
        }

        #endregion

        /// <summary>
        /// 是否按下急停按钮 true 未按下急停按钮 false 按下
        /// </summary>
        /// <returns></returns>
        public static bool GetStopDown()
        {
            return !arrInValue[14]; //true 未按下急停按钮 false 按下 取反方便理解
        }

        /// <summary>
        /// 设置电批是否可作业 计设置急停信号 true 可作业 非急停 false 不可作业 急停
        /// </summary>
        /// <returns></returns>
        public static bool SetDianPiCanWork(bool SetCanWorkState)
        {
            if (DeviceConState[2] == 1)//有IO控制器
            {
                bool bReturn = false;
                if (SetCanWorkState)//可作业 非急停
                {
                    if (nIsStop != 0)
                    {
                        nIsStop = 0;
                        bReturn = modbusIO.SetOutOpen(29);//非急停设1
                        System.Threading.Thread.Sleep(20);
                        return bReturn;
                    }
                }
                else//不可作业 急停
                {
                    if (nIsStop != 1)
                    {
                        nIsStop = 1;
                        bReturn = modbusIO.SetOutClose(29);//急停设0
                        System.Threading.Thread.Sleep(20);
                        return bReturn;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 电批启动信号
        /// </summary>
        /// <returns></returns>
        public static bool GetDianPiStart()
        {

            return arrInValue[12] || arrInValue[20]; 
        }

        /// <summary>
        /// 电批是否作业中
        /// </summary>
        /// <returns></returns>
        public static bool GetDianPiIsWork()
        {
            return arrInValue[13];
        }
        
        /// <summary>
        /// 电批作业返回结果
        /// </summary>
        /// <returns></returns>
        public static bool GetDianPiOKNG()
        {
            if (arrInValue[9] || arrInValue[10])
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 重置电批OK NG结果
        /// </summary>
        /// <returns></returns>
        public static bool DianPiOKNGClear()
        {
            modbusIO.SetOutOpen(39);
            Thread.Sleep(20);
            return modbusIO.SetOutClose(39);//重置电批OK NG结果
        }

        /// <summary>
        /// 设置电批正反转信号 true 正转 false 反转
        /// </summary>
        /// <returns></returns>
        public static bool SetDianPiZhengZhuan(bool SetZhengZhuan)
        {
            if (SetZhengZhuan)
            {
                modbusIO.SetOutOpen(19);//正传打开后 开始作业 即可关闭
                Thread.Sleep(500);
                return modbusIO.SetOutClose(19);//正转要关闭 
            }
            else
            {
                return modbusIO.SetOutOpen(18);//反转打开一直转
                //Thread.Sleep(20);
                //return modbusIO.SetOutClose(18);//反转关闭 就停止反转
            }
        }

        /// <summary>
        /// 电批停止反转
        /// </summary>
        /// <returns></returns>
        public static bool SetDianPiStopFanZhuan()
        {
            return modbusIO.SetOutClose(18);//反转关闭 就停止反转
        }

        /// <summary>
        /// 电批吸附开关 true开 false关
        /// </summary>
        /// <param name="isXiFuOpen"></param>
        /// <returns></returns>
        public static bool SetDianPiXiFu(bool isXiFuOpen)
        {
            if (isXiFuOpen)
            {
                return modbusIO.SetOutOpen(30);
            }
            else
            {
                return modbusIO.SetOutClose(30);
            }
        }


        #region DDK IO操作

        /// <summary>
        /// DDK 任务号IO
        /// </summary>
        static int[] DDKTaskNoIO = new int[5] { 24, 25, 26, 27, 28 };

        /// <summary>
        /// DDK 设置电批调用的任务号 1-32
        /// </summary>
        /// <param name="TaskNo"></param>
        public static void SetDDKTaskNo(int TaskNo)
        {
            switch (TaskNo)
            {
                case 1:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 2:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 3:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 4:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 5:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 6:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 7:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 8:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 9:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 10:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 11:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 12:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 13:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 14:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 15:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 16:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutClose(DDKTaskNoIO[4]);
                    break;
                case 17:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 18:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 19:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 20:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 21:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 22:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 23:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 24:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutClose(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 25:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 26:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 27:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 28:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutClose(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 29:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 30:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutClose(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 31:
                    modbusIO.SetOutClose(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
                case 32:
                    modbusIO.SetOutOpen(DDKTaskNoIO[0]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[1]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[2]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[3]);
                    modbusIO.SetOutOpen(DDKTaskNoIO[4]);
                    break;
            }
        }

        #endregion

        /// <summary>
        /// 三色灯蜂鸣 isOpen true亮 false灭 0红 1黄 2绿 3蜂鸣
        /// </summary>
        /// <param name="LedNum"></param>
        /// <returns></returns>
        public static bool SetLed(bool isOpen, int LedNum)
        {
            bool bReturn = false;
            if (isOpen)
            {
                if (LedState[LedNum] != 1)//亮
                {
                    LedState[LedNum] = 1;
                    bReturn = modbusIO.SetOutOpen(LedNum );//0红 1黄 2绿 3蜂鸣
                    System.Threading.Thread.Sleep(20);
                    return bReturn;
                }
                return true;
            }
            else
            {
                if (LedState[LedNum] != 0)//灭
                {
                    LedState[LedNum] = 0;
                    bReturn = modbusIO.SetOutClose(LedNum);//0红 1黄 2绿 3蜂鸣
                    System.Threading.Thread.Sleep(20);
                    return bReturn;
                }
                return true;
            }
        }

        /// <summary>
        /// 获取所有0-15位in值
        /// </summary>
        /// <param name="InIndex"></param>
        /// <returns></returns>
        public static bool[] GetInAllValue()
        {
            arrInValue = modbusIO.GetInValueAll();
            return arrInValue;
        }

        /// <summary>
        /// 开锁 123 200ms自动弹开 4延时关闭
        /// </summary>
        /// <param name="nLockNum"></param>
        /// <returns></returns>
        public static bool LockOpen(int nLockNum)
        {
            if (DeviceEnable[2])//有IO
            {
                if (nLockNum < 4)
                {
                    modbusIO.SetOutOpen(30 + nLockNum);
                    Thread.Sleep(200);
                    modbusIO.SetOutClose(30 + nLockNum);
                }
                else if (nLockNum == 4)
                {
                    modbusIO.SetOutOpen(34);
                    System.Threading.Thread thread = new System.Threading.Thread(() =>
                    {
                        Thread.Sleep(9999);
                        modbusIO.SetOutClose(34);
                    });
                    thread.Start();
                }
            }
            return false;
        }

        /// <summary>
        /// post请求方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="json">json参数</param>
        /// <returns></returns>
        public static string PostHtmlByJson(string url, string json)
        {
            string result = string.Empty;
            //object obj;
            try
            {
                var request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
                if (request != null)
                {
                    request.ContentType = "application/json";
                    request.Method = "post";

                    //request.CookieContainer = _cookie;
                    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                    {
                        streamWriter.Write(json);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                    System.Net.HttpWebResponse response;
                    try
                    {
                        response = (System.Net.HttpWebResponse)request.GetResponse();

                    }
                    catch (System.Net.WebException e)
                    {
                        response = e.Response as System.Net.HttpWebResponse;
                    }
                    if (response != null)
                    {
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }
                else result = "error:无效的URL！";
            }
            catch (UriFormatException ex)
            {
                result = "error:" + ex.Message;
            }
            catch (System.Net.WebException ex)
            {
                result = "error:" + ex.Message;
            }
            return result;
        }

        static string strUseFileName = System.IO.Directory.GetCurrentDirectory() + "\\Log\\Use\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
        static string strMESFileName = System.IO.Directory.GetCurrentDirectory() + "\\Log\\MES\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

        /// <summary>
        /// 写日志文件 1操作 2MES
        /// </summary>
        //async
        public static void WriteLog(int nType,string strInfo)
        {
            try
            {
                if (nType == 1)
                {
                    lock (strUseFileName)
                    {
                        if (!System.IO.File.Exists(strUseFileName))
                        {
                            FileStream fileStream = new FileStream(strUseFileName, FileMode.Create);
                            fileStream.Close();
                        }
                        StreamWriter streamWriter = File.AppendText(strUseFileName);
                        //await streamWriter.WriteLineAsync(DateTime.Now.ToString("HH:mm:ss.ff ") + strInfo);
                        streamWriter.WriteLineAsync(DateTime.Now.ToString("HH:mm:ss.ff ") + strInfo);
                        streamWriter.Close();
                    }
                }
                else
                {
                    lock (strMESFileName)
                    {
                        if (!System.IO.File.Exists(strMESFileName))
                        {
                            FileStream fileStream = new FileStream(strMESFileName, FileMode.Create);
                            fileStream.Close();
                        }
                        StreamWriter streamWriter = File.AppendText(strMESFileName);
                        //await streamWriter.WriteLineAsync(DateTime.Now.ToString("HH:mm:ss.ff ") + strInfo);
                        streamWriter.WriteLineAsync(DateTime.Now.ToString("HH:mm:ss.ff ") + strInfo);
                        //streamWriter.WriteLineAsync("-----------------------------------------------------------------------------------------------------------------------------------------------------------------");
                        streamWriter.Close();
                    }
                }
            }
            catch (Exception)
            {
                string strFileName = System.IO.Directory.GetCurrentDirectory() + "\\Log\\Error.txt";
                if (!System.IO.File.Exists(strFileName))
                {
                    FileStream fileStream = new FileStream(strFileName, FileMode.Create);
                    fileStream.Close();
                }
                StreamWriter streamWriter = File.AppendText(strFileName);
                streamWriter.WriteLineAsync(DateTime.Now.ToString("HH:mm:ss.ff ") + strInfo);
                streamWriter.Close();
            }
        }
    }
}
